package com.my.mygaode

import com.google.gson.Gson
import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type

object JsonParseUtil {

    fun <T> String.toBeanList(clazz: Class<*>): List<T> =
        Gson().fromJson<List<T>>(this, ParameterizedTypeImpl(clazz))
    /**
     * 解析 json 转 list 数组
     */
    inline fun <reified T> getGsonList(data: String, valueType: Class<T>): List<T> {
        return data.toBeanList(valueType)
    }

    class ParameterizedTypeImpl(var clz: Class<*>) : ParameterizedType {

        override fun getRawType(): Type = List::class.java

        override fun getOwnerType(): Type? = null

        override fun getActualTypeArguments(): Array<Type> = arrayOf(clz)
    }
}